<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Api\User\UCenterController;
use App\Http\Controllers\ApiController;
use App\Http\Models\User\UserBaseModel;
use App\Models\Order;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class FinanceController extends Controller
{
    public $pagniate = 15;
    public function index()
    {
        return view('admin.finance.index');
    }

    /**用户订单列表的返回
     * @param Request $request
     * @param Order $orderModel
     */
    public function user_order_list(Request $request,Order $orderModel)
    {
        $order_list = $orderModel -> admin_order_list($this->pagniate,$request->all());
        return ApiController::response($order_list,ApiController::SUC_CODE,'获取数据成功');
    }

    /**获取用户的收货地址列表
     * @param Request $request
     * @param Order $orderModel
     * @return $this
     */
    public function user_address_list(Request $request,Order $orderModel)
    {
        $userCenterController = new UCenterController();
        return $userCenterController -> addressList($request);
    }


    public function UserListWithPage(Request $request)
    {
        $input = $request->input();
        if (isset($input['key'])&&isset($input['val'])){
            $tmp = UserBaseModel::query()->where($input['key'],'like','%'.$input['val'].'%')->paginate(15);
        }else{
            $tmp = UserBaseModel::paginate(15);
        }
        if (!empty($tmp)){
            $tmp = $tmp->toArray();
            $data['current_page']     = $tmp['current_page'];
            $data['per_page']         = $tmp['per_page'];
            $data['total']            = $tmp['total'];
            $data['list']             = $tmp['data'];
            foreach ($data['list'] as $k=>$v){
                $data['list'][$k]['High_BNF']['BNF_Lv1']          = $this->getHighBnf($v['BNF_Lv1']);
                $data['list'][$k]['High_BNF']['BNF_Lv2']          = $this->getHighBnf($v['BNF_Lv2']);
                $data['list'][$k]['High_BNF']['BNF_Lv3']          = $this->getHighBnf($v['BNF_Lv3']);
            }
        }else{
            $data = [];
        }
        return ApiController::response($data);
    }


    private function getHighBnf($uid)
    {
        if ($uid){
            return UserBaseModel::getUser($uid);
        }else{
            return [];
        }
    }


    public function getLowerBNF(Request $request)
    {
        $uid            = $request->input('uid');
        $data           = [];
        if ($uid){
            $data['Lower_BNF_Lv1'] = $this->getLowerBnfFormat('Lower_BNF_Lv1',$uid);
            $data['Lower_BNF_Lv2'] = $this->getLowerBnfFormat('Lower_BNF_Lv2',$uid);
            $data['Lower_BNF_Lv3'] = $this->getLowerBnfFormat('Lower_BNF_Lv3',$uid);
        }else{
            $data['Lower_BNF_Lv1'] = [];
            $data['Lower_BNF_Lv2'] = [];
            $data['Lower_BNF_Lv3'] = [];
        }
        return ApiController::response($data);
    }


    private function getLowerBnfFormat($key,$uid)
    {
        $tmp       = UserBaseModel::query()->where(ltrim($key,'Lower_'),$uid)->get();
        if (!empty($tmp)){
            $tmp                         = $tmp->toArray();
            $data['list']          = $tmp??[];
            $data['current_page']  = $tmp['current_page']??0;
            $data['per_page']      = $tmp['per_page']??0;
            $data['total']         = $tmp['total']??0;
            return $data;
        }else{
            return  [];
        }
    }



}
